package com._21cn.product.utils;

import com._21cn.cms.common.Constants;
import com._21cn.cms.resource.utils.PictureUtil;
import com._21cn.cms.site.entity.RegionObject;
import com._21cn.cms.site.service.RegionObjectService;
import com._21cn.framework.utils.DateUtil;
import com._21cn.product.entity.*;
import com._21cn.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/19.
 */
@Component("productRelateHelper")
public class ProductRelateHelper {

    @Autowired
    private CateService cateService;

    @Autowired
    private CarPropertyService carPropertyService;

    @Autowired
    private CarCateService carCateService;

    @Autowired
    private CarPictureService carPictureService;

    @Autowired
    private CarBusinessPledgeService carBusinessPledgeService;

    @Autowired
    private CarBusinessSecondService carBusinessSecondService;

    @Autowired
    private RegionObjectService regionObjectService;

    public List<Cate> getAllOtherList(){
        Integer typeId = Constants.CATE_TYPE_OTHER;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqTypeId", typeId);
        params.put("eqStatus", Constants.STATUS_ENABLE);
        List<Cate> list = cateService.getList(params, 1, 1000, false);
        return list;
    }

    /**
     * 汽车属性
     * @param propertyMap
     * @param carId
     * @param typeId
     */
    public void insertPropertyMap(Map<Integer, String> propertyMap, Integer carId, Integer typeId){
        Date date = new Date();
        updatePropertyMap(propertyMap, carId, typeId);
    }

    public void updatePropertyMap(Map<Integer, String> propertyMap, Integer carId, Integer typeId){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqCarId", carId);
        params.put("eqTypeId", typeId);
        carPropertyService.delete(params);
        Date date = new Date();
        for(Integer key : propertyMap.keySet()){
            String propertyValue = propertyMap.get(key);
            insertCarProperty(key, propertyValue, carId, typeId, date);
        }
    }

    private void insertCarProperty(Integer propertyId, String propertyValue, Integer carId, Integer typeId, Date date){
        CarProperty carProperty = new CarProperty();
        carProperty.setPropertyId(propertyId);
        carProperty.setTypeId(typeId);
        carProperty.setCarId(carId);
        carProperty.setPropertyValue(propertyValue);
        carProperty.setCreateTime(date);
        carProperty.setModifyTime(date);
        carProperty.setStatus(Constants.STATUS_ENABLE);
        carPropertyService.insert(carProperty);
    }

    /**
     * 汽车类别
     * @param cateIdList
     * @param carId
     * @param typeId
     */
    public void updateCarCateList(List<Integer> cateIdList, Integer carId, Integer typeId){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqCarId", carId);
        params.put("eqTypeId", typeId);
        carCateService.delete(params);
        insertCarCateList(cateIdList, carId, typeId);
    }

    public void insertCarCateList(List<Integer> cateIdList, Integer carId, Integer typeId){
        for(Integer cateId : cateIdList){
            insertCarCate(cateId, carId, typeId);
        }
    }

    private void insertCarCate(Integer cateId, Integer carId, Integer typeId){
        Date date = new Date();
        CarCate carCate = new CarCate();
        carCate.setModifyTime(date);
        carCate.setCreateTime(date);
        carCate.setCarId(carId);
        carCate.setCateId(cateId);
        carCate.setTypeId(typeId);
        carCate.setStatus(Constants.STATUS_ENABLE);
        carCateService.insert(carCate);
    }

    public void updatePicList( List<String> picList, Integer carId ){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqCarId", carId);
        carPictureService.delete(params);
        insertPicList(picList, carId);
    }

    public void insertPicList( List<String> picList, Integer carId ){
        int i = 1;
        for (String picUrl : picList) {
            insertCarPicture(carId, picUrl, i);
            i++;
        }
    }

    private void insertCarPicture(Integer carId, String picUrl, int orderNum){
        Date date = new Date();
        CarPicture carPicture = new CarPicture();
        carPicture.setCarId(carId);
        carPicture.setCreateTime(date);
        carPicture.setPicUrl(picUrl);
        String hashCode = PictureUtil.getHashCodeFromUrl(carPicture.getPicUrl());
        carPicture.setPicHashcode(hashCode);
        carPicture.setOrderNum(orderNum);
        carPicture.setStatus(Constants.STATUS_ENABLE);
        carPictureService.insert(carPicture);
    }

    /**
     * 汽车业务
     * @param carId
     */
    public void updatePledgeCar( Integer carId, Float secondPrice, Float odoMeter, Date dateOnBoard, String areaOnBoard){
        CarBusinessPledge carBusinessPledge = new CarBusinessPledge();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqCarId", carId);
        List<CarBusinessPledge> list = carBusinessPledgeService.getList(params, 1, 1, false);
        if( list.size()>0 ){
            carBusinessPledge = list.get(0);
            carBusinessPledge.setSecondPrice(secondPrice);
            carBusinessPledge.setOdoMeter(odoMeter);
            carBusinessPledge.setDateOnBoard(dateOnBoard);
            carBusinessPledge.setAreaOnBoard(areaOnBoard);
            carBusinessPledgeService.update(carBusinessPledge);
        }else {
            carBusinessPledge.setCarId(carId);
            carBusinessPledge.setSecondPrice(secondPrice);
            carBusinessPledge.setOdoMeter(odoMeter);
            carBusinessPledge.setDateOnBoard(dateOnBoard);
            carBusinessPledge.setAreaOnBoard(areaOnBoard);
            carBusinessPledgeService.insert(carBusinessPledge);
        }
    }

    public void updateSecondCar( Integer carId, Float secondPrice, Float odoMeter,  Date dateOnBoard, String areaOnBoard){
        CarBusinessSecond carBusinessSecond = new CarBusinessSecond();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqCarId", carId);
        List<CarBusinessSecond> list = carBusinessSecondService.getList(params, 1, 1, false);
        if( list.size()>0 ){
            carBusinessSecond = list.get(0);
            carBusinessSecond.setSecondPrice(secondPrice);
            carBusinessSecond.setDateOnBoard(dateOnBoard);
            carBusinessSecond.setAreaOnBoard(areaOnBoard);
            carBusinessSecond.setOdoMeter(odoMeter);
            carBusinessSecondService.update(carBusinessSecond);
        }else {
            carBusinessSecond.setCarId(carId);
            carBusinessSecond.setSecondPrice(secondPrice);
            carBusinessSecond.setDateOnBoard(dateOnBoard);
            carBusinessSecond.setAreaOnBoard(areaOnBoard);
            carBusinessSecond.setOdoMeter(odoMeter);
            carBusinessSecondService.insert(carBusinessSecond);
        }
    }

    public CarBusinessSecond getCarBusinessSecond( Integer carId ){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqCarId", carId);
        List<CarBusinessSecond> list = carBusinessSecondService.getList(params, 1, 1, false);
        if( list.size()>0 ){
            return list.get(0);
        }
        return new CarBusinessSecond();
    }

    public CarBusinessPledge getCarBusinessPledge( Integer carId ){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqCarId", carId);
        List<CarBusinessPledge> list = carBusinessPledgeService.getList(params, 1, 1, false);
        if( list.size()>0 ){
            return list.get(0);
        }
        return new CarBusinessPledge();
    }

    public void deleteCarRelate(List<Integer> carIdList){
        for( Integer carId : carIdList ){
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("eqCarId", carId);
            carBusinessPledgeService.delete(params);
            carBusinessSecondService.delete(params);
            carCateService.delete(params);
            carPropertyService.delete(params);
            carPictureService.delete(params);
        }
    }

    public void push2RegionObj(Integer regionId, Integer objectId, Integer objectType){
        Integer topTime = DateUtil.getCurrent10TimeStamp();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqRegionId", regionId);
        params.put("eqObjectId", objectId);
        params.put("eqObjectType", objectType);
        List<RegionObject> list = regionObjectService.getList(params, 1, 1, false);
        if( !list.isEmpty()){
            RegionObject regionObject = list.get(0);
            regionObject.setModifyTime(new Date());
            regionObject.setTopTime(topTime);
            regionObjectService.update(regionObject);
        }else {
            RegionObject regionObject = new RegionObject();
            regionObject.setModifyTime(new Date());
            regionObject.setTopTime(topTime);
            regionObject.setCreateTime(new Date());
            regionObject.setObjectId(objectId);
            regionObject.setObjectType(objectType);
            regionObject.setRegionId(regionId);
            regionObject.setStatus(Constants.STATUS_ENABLE);
            regionObjectService.insert(regionObject);
        }

    }

}
